use shq_common::prelude::{get_my_pool, TreeNode, Zjfp};
use sqlx::Row;

//获取条线
pub async fn zxjl_conf_zb() -> anyhow::Result<Vec<TreeNode>> {
    let pool: &'static sqlx::Pool<sqlx::MySql> = get_my_pool();
    let mut tr = pool.begin().await?;
    let sql = "SELECT ZBID, ZBMC FROM pas_oltp_sh.SH_JHCW_ZXJL_ZB ";
    let rows = sqlx::query(sql).fetch_all(&mut *tr).await?;
    let mut result = Vec::new();
    for row in rows.into_iter() {
        let zbid: i32 = row.try_get(0)?;
        let zbmc: String = row.try_get(1)?;
        result.push(TreeNode {
            title: zbmc.clone(),
            key: zbid.to_string(),
            parent_key: None,
        });
    }
    tr.commit().await?;
    Ok(result)
}

//保存分配
pub async fn zxjl_fp_insert(
    ny: i32,
    cgid: i32,
    yfpe: f64,
    emid: i32,
    ffny: i32,
    zjmc: String,
    txid: i32,
) -> anyhow::Result<()> {
    let sql = "UPDATE pas_oltp_sh.SH_JHCW_ZHZJ SET yfpe = ? WHERE ny = ? AND cgid = ? ";
    sqlx::query(sql)
        .bind(yfpe)
        .bind(ny)
        .bind(cgid)
        .execute(get_my_pool())
        .await?;
    let sql = "UPDATE pas_oltp_sh.SH_JHCW_ZHZJ SET sye = jle - yfpe  WHERE ny = ? AND cgid = ?";
    sqlx::query(sql)
        .bind(ny)
        .bind(cgid)
        .execute(get_my_pool())
        .await?;
    let sql = "SELECT COUNT(*) FROM pas_oltp_sh.SH_JHCW_ZHZJ_FPJG WHERE CGID = ?";
    let count: (i32,) = sqlx::query_as(sql)
        .bind(cgid)
        .fetch_one(get_my_pool())
        .await?;
    if count.0 > 0 {
        let sql = "UPDATE pas_oltp_sh.SH_JHCW_ZHZJ_FPJG SET FPJE = ? WHERE CGID = ?";
        sqlx::query(sql)
            .bind(yfpe)
            .bind(cgid)
            .execute(get_my_pool())
            .await?;
    } else {
        let sql = "INSERT INTO pas_oltp_sh.SH_JHCW_ZHZJ_FPJG
            ( FFNY, CGID, ZJMC, FPR_EMID, TXID, ZT, FPJE)
            VALUES( ?, ?, ?, ?, ?, 1, ?)";
        sqlx::query(sql)
            .bind(ffny)
            .bind(cgid)
            .bind(zjmc)
            .bind(emid)
            .bind(txid)
            .bind(yfpe)
            .execute(get_my_pool())
            .await?;
    }
    Ok(())
}

//获取分配结果
pub async fn zxjl_fpjg(cgid: i32) -> anyhow::Result<Vec<Zjfp>> {
    let cgid = cgid as i32;
    let sql = "SELECT zjmc,fpje,ffny,txid,fpr_emid,emnm
                    FROM pas_oltp_sh.SH_JHCW_ZHZJ_FPJG A
                    LEFT JOIN v_user_name B ON A.fpr_emid = B.id 
                    WHERE cgid = ?";
    let rows = sqlx::query(sql)
        .bind(&cgid)
        .fetch_all(get_my_pool())
        .await?;
    let mut res: Vec<Zjfp> = Vec::new();
    for row in rows.iter() {
        let zjmc: String = row.try_get(0)?;
        let fpje: rust_decimal::Decimal = row.try_get(1)?;
        let ffny: i32 = row.try_get(2)?;
        let txid: i32 = row.try_get(3)?;
        let fpr_emid: i32 = row.try_get(4)?;
        let fpr_emnm: String = row.try_get(5)?;
        res.push(Zjfp {
            zjmc,
            fpje,
            ffny,
            txid,
            fpr_emid,
            fpr_emnm,
        });
    }
    Ok(res)
}

//是否终止
pub async fn zxjl_zz(ny: i32) -> anyhow::Result<()> {
    let sql = "UPDATE pas_oltp_sh.SH_JHCW_ZHZJ SET iszz = 1 WHERE ny = ?";
    sqlx::query(sql).bind(ny).execute(get_my_pool()).await?;
    Ok(())
}
